package com.tdam2014.grupo3;

import java.util.ArrayList;

import android.app.ListFragment;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.tdam2014.grupo3.adapters.PhotoSetAdapter;
import com.tdam2014.grupo3.db.PhotoSetDatabaseHelper;
import com.tdam2014.grupo3.entities.PhotoSet;
import com.tdam2014.grupo3.interfaces.PhotoSetSelectionListener;
import com.tdam2014.grupo3.interfaces.UpdateableUI;
import com.tdam2014.grupo3.tasks.UpdatePhotoSetsListTask;

public class PhotoSetFragment extends ListFragment implements UpdateableUI {
	
	private PhotoSetSelectionListener photoSetSelectionListener;
	private Menu photoSetMenu;
	private String peopleNsid;

	public PhotoSetFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		setHasOptionsMenu(true);
		
		View rootView = inflater.inflate(R.layout.fragment_photo_set, container, false);
		return rootView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		peopleNsid = getActivity().getIntent().getStringExtra(PeopleActivity.KEY_PEOPLE_NSID);
		
		ArrayList<PhotoSet> photoSets = null;
		if(peopleNsid != null) photoSets = new PhotoSetDatabaseHelper(getActivity().getApplicationContext()).getPhotoSetList(peopleNsid);
		
		final PhotoSetAdapter photoSetAdapter = new PhotoSetAdapter(getActivity().getApplicationContext());
		photoSetAdapter.setPhotoSets(photoSets);
		setListAdapter(photoSetAdapter);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				PhotoSet photoSet = (PhotoSet) photoSetAdapter.getItem(position);
				photoSetSelectionListener.onPhotoSetSelected(photoSet);
			}
		});
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.update_options, menu);
		this.photoSetMenu = menu;
		
		if(getListAdapter().getCount() == 0) updatePhotoSets();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
			case android.R.id.home:
		        NavUtils.navigateUpFromSameTask(getActivity());
		        break;
			case R.id.update:
				updatePhotoSets();
				break;
		}
		
		return true;
	}
	
	@Override
	public PhotoSetAdapter getListAdapter() {
		return (PhotoSetAdapter) super.getListAdapter();
	}

	public void setPhotoSetSelectionListener(
			PhotoSetSelectionListener photoSetSelectionListener) {
		this.photoSetSelectionListener = photoSetSelectionListener;
	}

	private void updatePhotoSets() {
		if(peopleNsid != null) {
			UpdatePhotoSetsListTask updatePhotoSetsTask = new UpdatePhotoSetsListTask(getActivity(), peopleNsid, this);
			updatePhotoSetsTask.execute(peopleNsid);
		}
	}

	@Override
	public void onStartUpdate() {
		((TextView) getListView().getEmptyView()).setText(getResources().getString(R.string.updating) + " " + getResources().getString(R.string.albums) + "...");
		
		photoSetMenu.findItem(R.id.update).setVisible(false);
		getActivity().setProgressBarIndeterminateVisibility(true);
		
		ArrayList<PhotoSet> photoSets = new ArrayList<PhotoSet>();
		getListAdapter().setPhotoSets(photoSets);
		getListAdapter().notifyDataSetChanged();
	}

	@Override
	public void onFinishUpdate() {
		((TextView) getListView().getEmptyView()).setText(getResources().getString(R.string.empty_list_photosets));
		
		ArrayList<PhotoSet> photoSets = new PhotoSetDatabaseHelper(getActivity().getApplicationContext()).getPhotoSetList(peopleNsid);
		getListAdapter().setPhotoSets(photoSets);
		getListAdapter().notifyDataSetChanged();
		
		getActivity().setProgressBarIndeterminateVisibility(false);
		photoSetMenu.findItem(R.id.update).setVisible(true);
	}

	@Override
	public void informError(String error) {
		((TextView) getListView().getEmptyView()).setText(error);
		
		ArrayList<PhotoSet> photoSets = new PhotoSetDatabaseHelper(getActivity().getApplicationContext()).getPhotoSetList(peopleNsid);
		getListAdapter().setPhotoSets(photoSets);
		getListAdapter().notifyDataSetChanged();
		
		getActivity().setProgressBarIndeterminateVisibility(false);
		photoSetMenu.findItem(R.id.update).setVisible(true);
		
		if(!photoSets.isEmpty()) Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
	}
}